<template>
  <div class="page-box">
    <div class="page-header">
      <div class="back" @click="goback">
        <el-icon><ArrowLeftBold /></el-icon>
      </div>
      <div class="title">结算</div>
    </div>
    <div class="page-content">
      <div class="content-box">
        <div class="table-box">
          <div class="table-title">当前桌号：</div>
          <div class="table-index">
            <span> {{ tableId }}</span>
          </div>
        </div>
        <div class="goods-list">
          <template v-for="(item, index) in goodsList" :key="index">
            <div class="goods-item">
              <div class="goods-img">
                <img :src="item.url" />
              </div>
              <div class="goods-title">
                {{ item.name }}
              </div>
              <div class="price-box">
                <div class="price">
                  <span>￥</span>
                  <span>{{ (item.price / 100).toFixed(2) }}</span>
                </div>
                <div class="num">x{{ item.num }}</div>
              </div>
            </div>
          </template>
        </div>
      </div>
      <div class="bottom-box">
        <div class="bottom-price-box">
          <span>总计</span>
          <span>￥</span>
          <span>{{ (sumPrice / 100).toFixed(2) }}</span>
        </div>
        <el-button type="primary" @click="settleOrder">支付</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useGoodsStore } from "@/stores/modules/goods";
import { useMobileStore } from "@/stores/modules/mobile";
import { createOrder } from "@/api/modules/mobile";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const goodsStore = useGoodsStore();
const router = useRouter();

const goodsList = ref<any[]>(goodsStore.orderInfo.goodsList);
const sumPrice = ref<any>(goodsStore.orderInfo.sumPrice);
const tableId = useMobileStore().tableId;

const settleOrder = async () => {
  const params = {
    userId: useMobileStore().userId,
    tableId: useMobileStore().tableId,
    totalAmount: sumPrice.value,
    status: 2,
    list: goodsList.value.map(item => {
      return {
        menuId: item.id,
        menuName: item.name,
        quantity: item.num,
        price: item.price
      };
    })
  };
  ElMessage.success("支付成功！");
  await createOrder(params);
  router.replace("/mobile/home");
};

const goback = () => {
  router.go(-1);
};
</script>
<style lang="scss" scoped>
.page-box {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #f3f0f0;
  .page-header {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 40px;
    background-color: #ffffff;
    border-bottom: 1px solid #333333;
    .back {
      position: absolute;
      top: 12px;
      left: 12px;
    }
  }
  .page-content {
    width: 100%;
    height: calc(100% - 60px);
    background-color: #f3f0f0;
    .content-box {
      width: calc(100% - 48px);
      height: auto;
      padding: 12px;
      margin: 12px 12px 0;
      background-color: #ffffff;
      border-radius: 8px;
      box-shadow: 0 2px 5px 1px #33333350;
      .table-box {
        display: flex;
        flex-direction: row;
        width: 100%;
      }
      .goods-list {
        width: 100%;
        height: auto;
        margin-top: 8px;
        .goods-item {
          display: flex;
          justify-content: space-between;
          width: 100%;
          margin-bottom: 12px;
          .goods-img {
            width: 60px;
            height: 60px;
            background-color: #eeeeee;
          }
          .goods-title {
            flex: 1;
            margin: 0 12px;
            font-size: 14px;
          }
          .price-box {
            display: flex;
            flex-direction: column;
            align-items: flex-end;
          }
        }
      }
    }
    .bottom-box {
      position: absolute;
      bottom: 0;
      left: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: calc(100% - 24px);
      height: 60px;
      padding: 12px;
      background-color: #ffffff;
    }
  }
}
</style>
